<?php

namespace Member\Controller;

/**
 * 黑名单 控制器类
 *
 * @since 1.0 <2017-3-24> SoChishun <14507247@qq.com> Added.
 */
class Blacklist extends AppbaseController {

    function indexSearch($category) {
        $search = I('param.');
        $where = array();
        if (!empty($search['word'])) {
            $where['content'] = array('like', '%' . $search['word'] . '%');
        }
        if (!empty($category)) {
            $search['category_name'] = $category;
            $where['category_name'] = $category;
        }
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    function indexView($category) {
        $asearch = $this->indexSearch($category);
        $mBlacklist = new \XWAM\Model\BlacklistModel();
        $list = $mBlacklist->selectPaging($page, array('where' => $asearch['where'], 'order' => 'sort, id desc'), array('page_params' => $asearch['search']));
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->assign('tabs', $this->getTabs());
        $this->display();
    }

    function index() {
        $this->indexView('ip');
    }

    function ip() {
        $this->indexView(ACTION_NAME);
    }

    function email() {
        $this->indexView(ACTION_NAME);
    }

    function username() {
        $this->indexView(ACTION_NAME);
    }

    function url() {
        $this->indexView(ACTION_NAME);
    }

    function edit($id = 0, $category = '') {
        $data = array();
        if ($id) {
            $mBlacklist = new \XWAM\Model\BlacklistModel();
            $data = $mBlacklist->find($id);
        }
        if (!$data) {
            $data = array('category_name' => $category, 'user_name' => $this->userName, 'site_id' => $this->siteId);
        }
        $this->assign('data', $data);
        $this->display();
    }

    function editSave() {
        $mBlacklist = new \XWAM\Model\BlacklistModel();
        $result = $mBlacklist->saveBlacklist();
        $this->dialogJump($result['status'], $result['info']);
    }

    function changeStatus($id, $status) {
        $mBlacklist = new \XWAM\Model\BlacklistModel();
        $this->ajaxReturn($mBlacklist->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mBlacklist = new \XWAM\Model\BlacklistModel();
        $this->ajaxReturn($mBlacklist->changeSort($id, $sort));
    }

    function delete($id = '') {
        $mBlacklist = new \XWAM\Model\BlacklistModel();
        $result = $mBlacklist->deleteBlacklist($id);
        $this->ajaxMsg($result['status'], $result['info']);
    }

    // 2017-3-20
    function category($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mBlacklist = new \XWAM\Model\BlacklistModel();
        $list = $mBlacklist->where(array('id' => array('in', $id)))->field('id, title, category_id')->order('sort, id desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 保存类别
     * @since 1.0 <2016-3-20> SoChishun Added.
     */
    public function categorySave() {
        $aid = I('id');
        $aCategoryId = I('category_id');
        $mBlacklist = new \XWAM\Model\BlacklistModel();
        $result = $mBlacklist->saveCategory($aid, $aCategoryId);
        if (!$result['status']) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $this->dialogJump(true);
    }

    // 2017-3-24
    protected function getTabs($name = '') {
        if (!$name) {
            $name = ACTION_NAME;
        }
        $data = array(
            'index' => array('title' => 'IP', 'url' => 'index', 'active' => ''),
            'username' => array('title' => '帐号', 'url' => 'username', 'active' => ''),
            'email' => array('title' => '邮箱地址', 'url' => 'email', 'active' => ''),
            'url' => array('title' => '网址', 'url' => 'url', 'active' => ''),
        );
        if (array_key_exists($name, $data)) {
            $data[$name]['active'] = 'active';
        }
        return $data;
    }

    // 2017-3-24
    function getCategoryTreeJson() {
        $data = array('ip' => 'IP', 'username' => '帐号', 'email' => '邮件地址', 'url' => '网址');
        $this->ajaxReturn($data);
    }

}
